<?php
/*
Plugin Name: Sub Categories Widget
Description: This Widget lists the sub-categories for a category.
Author: BrokenCrust
Version: 1.0
Author URI: http://fanshuai.sinaapp.com/
Plugin URI: 
License: GPLv2 or later
*/

/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

class MineralSubCategoriesWidget extends WP_Widget {

	function MineralSubCategoriesWidget() {
		parent::WP_Widget('mineral_sub_categories_widget',
						__('Mineral Sub Categories', 'text_domain'),
						array('description' => __('A simple widget of showing root category', 'text_domain')));
	}

	/**
	 * Front-end display of widget
	 * 
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	function widget($args, $instance) {
		extract($args, EXTR_SKIP);

		$current_cat_id = 0;
		if (is_category()) {
			$current_cat_id = $this->get_current_category();
		} else if (is_single()) {
			$current_cat_id = $this->get_post_category();
		} else {
			return;
		}
		$root_cat_id = $this->get_category_root_id($current_cat_id);

		$sub_categories_args = array(
			'child_of'		=>	$root_cat_id,
			'orderby'		=>	'name',
			'order'			=>	'ASC',
			'hide_empty'	=>	0
		);

		echo '<div class="sidebar-box widget_categories"><ul>';
		$categories = get_categories($sub_categories_args);
		foreach ($categories as $category) {
			echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>';
		}
		echo '</ul></div>';

	}

	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 * 
	 * @return array Updated safe values to be save.
	 */
	function update($new_instance, $old_instance) {
		return $new_instance;
	}

	/**
	 * Back-end widget form.
	 *
	 * @param array $instance Previously saved values from database.
	 */
	function form($instance) {

	}

	function get_current_category() {
		global $post, $wp_query;

		return $cat = get_query_var('cat');
	}

	function get_category_root_id($cat) {
		$this_category = get_category($cat);
		while($this_category->category_parent) {
			$this_category = get_category($this_category->category_parent);
		}
		return $this_category->term_id;
	}

	function get_post_category() {
		$cats = get_the_category();

		return $cats[0]->cat_ID;
	}
}

add_action('widgets_init', create_function('', 'return register_widget("MineralSubCategoriesWidget");'));

?>
